package com.gvitz.transaction.activities;

import com.gvitz.transaction.R;
import com.gvitz.transaction.database.webserviceTransaction;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class ConfirmActivity extends GVActivity {

	String userEntered;
	
	final int PIN_LENGTH = 4;
	boolean keyPadLockedFlag = false;


	TextView pinBox0;
	TextView pinBox1;
	TextView pinBox2;
	TextView pinBox3;
	
	TextView [] pinBoxArray;
	TextView monto;
	
	
	Button button0;
	Button button1;
	Button button2;
	Button button3;
	Button button4;
	Button button5;
	Button button6;
	Button button7;
	Button button8;
	Button button9;
	Button button10;
	Button buttonExit;
	Button buttonDelete;
	Button buttonPagar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//iniciar variables
		

		userEntered = "";
		final Bundle extras = getIntent().getExtras(); 
			
		setContentView(R.layout.activity_confirm);	
		monto = (TextView)findViewById(R.id.precio_confirmar);
		monto.setText("$" + extras.getString("monto"));
				
		buttonExit = (Button) findViewById(R.id.buttonExit);
		buttonExit.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	finish();
		        
		    }
		    
		    }
		);
		
		
		buttonDelete = (Button) findViewById(R.id.buttonDeleteBack);
		buttonDelete.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    
		    	if (keyPadLockedFlag == true)
		    	{
		    		return;
		    	}
		    	
		    	if (userEntered.length()>0)
		    	{
		    		userEntered = userEntered.substring(0,userEntered.length()-1);
		    		pinBoxArray[userEntered.length()].setText("");
		    	}
		    
		    
		    }
		    
		    }
		);
		
		
		buttonPagar = (Button) findViewById(R.id.buttonpagar);
		buttonPagar.setOnClickListener(new View.OnClickListener() {
		    public void onClick(View v) {
		    	
		    	if(userEntered.length()==4){
				 		
		    		   webserviceTransaction t = new webserviceTransaction(getApplicationContext(),ConfirmActivity.this);
				 		t.makeTransaccion(extras.getString("monto"), 
				 				extras.getString("descripcion"), 
				 				extras.getString("id_usuario"),
				 				extras.getString("id_tarjeta"), 
				 				extras.getString("id_cliente"),
				 				userEntered);
				 		

			    }else{
			    	 Toast.makeText(getApplicationContext(),"Debe ingresar 4 digitos", Toast.LENGTH_LONG).show();	
			    }
		   }
		  }
		);
				
		pinBox0 = (TextView)findViewById(R.id.pinBox0);
		pinBox1 = (TextView)findViewById(R.id.pinBox1);
		pinBox2 = (TextView)findViewById(R.id.pinBox2);
		pinBox3 = (TextView)findViewById(R.id.pinBox3);

		
		pinBoxArray = new TextView[PIN_LENGTH];
		pinBoxArray[0] = pinBox0;
		pinBoxArray[1] = pinBox1;
		pinBoxArray[2] = pinBox2;
		pinBoxArray[3] = pinBox3;
		
	
		View.OnClickListener pinButtonHandler = new View.OnClickListener() {
	
			public void onClick(View v) {
				
		    	
		    	if (keyPadLockedFlag == true)
		    	{
		    		return;
		    	}
		    	
		    	Button pressedButton = (Button)v;
	    		
		    	
		    	if (userEntered.length()<PIN_LENGTH)
		    	{
		    		userEntered = userEntered + pressedButton.getText();
		    		
		    		//Update pin boxes
		    		pinBoxArray[userEntered.length()-1].setText("8");	
		    	}		    	
		    }
		  };
		
		
		button0 = (Button)findViewById(R.id.button0);
		button0.setOnClickListener(pinButtonHandler);
		
		button1 = (Button)findViewById(R.id.button1);
		button1.setOnClickListener(pinButtonHandler);
		
		button2 = (Button)findViewById(R.id.button2);
		button2.setOnClickListener(pinButtonHandler);
				
		button3 = (Button)findViewById(R.id.button3);
		button3.setOnClickListener(pinButtonHandler);
		
		button4 = (Button)findViewById(R.id.button4);
		button4.setOnClickListener(pinButtonHandler);
		
		button5 = (Button)findViewById(R.id.button5);
		button5.setOnClickListener(pinButtonHandler);
		
		button6 = (Button)findViewById(R.id.button6);
		button6.setOnClickListener(pinButtonHandler);
		
		button7 = (Button)findViewById(R.id.button7);
		button7.setOnClickListener(pinButtonHandler);
		
		button8 = (Button)findViewById(R.id.button8);
		button8.setOnClickListener(pinButtonHandler);
		
		button9 = (Button)findViewById(R.id.button9);
		button9.setOnClickListener(pinButtonHandler);
				
		
        Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );

	       buttonDelete.setTypeface(font);
	       buttonExit.setTypeface(font);
	       buttonPagar.setTypeface(font);

		
		
	}
	
	@Override
	public void onBackPressed() {
		return;
	}

   
}
